﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace Enterprise.Management.Gridview
{
    public class GridviewCell
    {
        private Dictionary<string, string> properties { get; set; }

        public string Value { get; set; }
        public GridviewDataTemplate DataTemplate { get; set; }
        public GridviewDataFormatter Formatter { get; set; }

        public GridviewCell()
        {
            properties = new Dictionary<string, string>();
        }

        public virtual string Write()
        {
            return Write(null, null);
        }

        public virtual string Write(GridviewDataProvider provider, object obj)
        {
            if (string.IsNullOrEmpty(Value)) return string.Empty;

            string content;
            if (provider != null)
            {
                content = provider.Get(obj, Value).ToString();
            }
            else
            {
                content = Value;
            }

            if (Formatter != null)
            {
                content = Formatter.Format(content);
            }

            return content;
        }

        public void SetProperty(string name, string value)
        {
            if (properties.ContainsKey(name))
            {
                properties[name] = value;
            }
            else
            {
                properties.Add(name, value);
            }
        }

        public string GetPropertiesHtml()
        {
            var content = new StringBuilder();
            foreach (string key in properties.Keys)
            {
                content.Append(string.Format(@" {0}=""{1}""", key, properties[key]));
            }

            return content.ToString();
        }
    }
}